package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    //根据日期修改预约人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {//

        //和导入方法流程大致相同,少做了遍历操作

        long count=orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count>0){
            //当前日期已经进行了预约设置，需要进行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
       /* String dateBegin = date+"-1";   //2019-10-1
        String dateEnd = date+"-31";    //2019-10-31

        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);

        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        *//*List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());  //获得几号
            orderSettingMap.put("number",orderSetting.getNumber()); //可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations()); //已预约人数
            data.add(orderSettingMap);
        }*//*
        //可以利用Lamber表达式来解决OrderSetting->map
        List<Map<Object, Object>> collect = list.stream().map(s -> {
            HashMap<Object, Object> map1 = new HashMap<>();
            map1.put("date", s.getOrderDate().getDate());
            map1.put("number", s.getNumber());
            map1.put("reservations", s.getReservations());
            return map1;
        }).collect(Collectors.toList());*/

        /*在sql中利用DATE_FORMAT方法查询天 返回一个map集合*/
        List<Map> listmap = orderSettingDao.listmap(date);
        return listmap;
    }

    //为了避免用户重复导入文件,需要进行时间日期判断预约人数是否重复
    @Override
    public void add(List<OrderSetting> list) {
            if (list!=null && list.size()>0){
                //遍历,批量的添加
                for (OrderSetting orderSetting : list) {
                    //进行时间日期判断预约人数是否重复
                    long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                    if (countByOrderDate > 0) {
                        //如果查出的日期有值,则执行更新操作
                        orderSettingDao.editNumberByOrderDate(orderSetting);
                    }else {
                        //如果数据库没有对应的日期,则执行添加操作
                        orderSettingDao.add(orderSetting);
                    }
                }
            }
    }
}
